package com.dianning.web.action.user;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.*;
import org.apache.struts2.interceptor.SessionAware;

import com.dianning.bean.Parameter;
import com.dianning.bean.Resume;
import com.dianning.bean.UserDetail;
import com.dianning.bean.UserInfo;
import com.dianning.bean.Visitor;
import com.dianning.comm.Constant;
import com.dianning.comm.OrderObj;
import com.dianning.comm.Util;
import com.dianning.service.InfoService;
import com.dianning.service.ParameterService;
import com.dianning.service.UserInfoService;
import com.dianning.service.VisitorService;
import com.dianning.service.web.PageList;
import com.dianning.service.web.Pages;
import com.dianning.web.action.BaseAction;
import com.dianning.web.interceptor.UserSessionAware;
import com.dianning.web.servlet.UserSession;
import com.dianning.web.ui.OptionsInt;
import com.dianning.web.ui.OptionsLong;
import com.dianning.web.ui.RadioInt;

public class UserInfoAction extends BaseAction implements SessionAware,UserSessionAware{

	private static final Log logger = LogFactory.getLog(UserInfoAction.class);
	
	private UserInfoService userInfoService;
	private ParameterService parameterService;

	public UserInfoService getUserInfoService() {
		return userInfoService;
	}

	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}	
	
	public ParameterService getParameterService() {
		return parameterService;
	}

	public void setParameterService(ParameterService parameterService) {
		this.parameterService = parameterService;
	}

	private List sexList = new ArrayList();
	private List yearList = new ArrayList();
	private List monthList = new ArrayList();
	private List dayList = new ArrayList();
	private List edugradeList = new ArrayList();
	private List workyearsList = new ArrayList();

	public List getSexList() {
		return sexList;
	}

	public void setSexList(List sexList) {
		this.sexList = sexList;
	}

	public List getYearList() {
		return yearList;
	}

	public void setYearList(List yearList) {
		this.yearList = yearList;
	}

	public List getMonthList() {
		return monthList;
	}

	public void setMonthList(List monthList) {
		this.monthList = monthList;
	}

	public List getDayList() {
		return dayList;
	}

	public void setDayList(List dayList) {
		this.dayList = dayList;
	}

	public List getEdugradeList() {
		return edugradeList;
	}

	public void setEdugradeList(List edugradeList) {
		this.edugradeList = edugradeList;
	}

	public List getWorkyearsList() {
		return workyearsList;
	}

	public void setWorkyearsList(List workyearsList) {
		this.workyearsList = workyearsList;
	}
	
	private void setSexListValues(){
		sexList.add(new RadioInt(1, "女"));
		sexList.add(new RadioInt(2, "男"));
	}
	private void setYearListValues(){
		for(int i=1930;i<=Util.getYear();i++){
			yearList.add(new OptionsInt(i, String.valueOf(i)));
		}
	}
	private void setMonthListValues(){
		for(int i=1;i<=12;i++){
			monthList.add(new OptionsInt(i, String.valueOf(i)));
		}
	}
	private void setDayListValues(){
		for(int i=1;i<=31;i++){
			dayList.add(new OptionsInt(i, String.valueOf(i)));
		}
	}
	private void setEdugradeListValues(){
		List l = this.getParameterService().findParameters("edugrade", 2, null, "orders", 0);
		for(int i=0;i<l.size();i++){
			Parameter p = (Parameter) l.get(i);
			edugradeList.add(new OptionsLong(p.getId(), p.getParamName()));
		}
	}
	private void setWorkyearsListValues(){
		List l = this.getParameterService().findParameters("workyears", 2, null, "orders", 0);
		for(int i=0;i<l.size();i++){
			Parameter p = (Parameter) l.get(i);
			workyearsList.add(new OptionsLong(p.getId(), p.getParamName()));
		}
	}
	private void initResumeValues(){
		setSexListValues();
		setYearListValues();
		setMonthListValues();
		setDayListValues();
		setEdugradeListValues();
		setWorkyearsListValues();
	}
	
	private UserSession userSession;
	private Map session;

	public UserSession getUserSession() {
		return userSession;
	}

	public void setUserSession(UserSession userSession) {
		this.userSession = userSession;
	}
	
	public Map getSession() {
		return session;
	}
	
	public void setSession(Map session) {
		this.session = session;
	}
	
	private UserInfo userInfo;
	private String domain = Constant.DOMAIN;
	private Resume resume;

	public UserInfo getUserInfo() {
		return userInfo;
	}

	public void setUserInfo(UserInfo userInfo) {
		this.userInfo = userInfo;
	}
	
	public String getDomain() {
		return domain;
	}

	public void setDomain(String domain) {
		this.domain = domain;
	}
	
	public Resume getResume() {
		return resume;
	}

	public void setResume(Resume resume) {
		this.resume = resume;
	}
	
	private String name;
	private int sex;
	private String mobile;
	private String tel;
	private String qq;
	private String email;
	private int birthyear;
	private int birthmonth;
	private int birthday;
	private int edugrade;
	private int workyears;
	private String workExperience;
	private String studyExperience;
	private String oldpass;
	private String password;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getSex() {
		return sex;
	}

	public void setSex(int sex) {
		this.sex = sex;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	public String getQq() {
		return qq;
	}

	public void setQq(String qq) {
		this.qq = qq;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public int getBirthyear() {
		return birthyear;
	}

	public void setBirthyear(int birthyear) {
		this.birthyear = birthyear;
	}

	public int getBirthmonth() {
		return birthmonth;
	}

	public void setBirthmonth(int birthmonth) {
		this.birthmonth = birthmonth;
	}

	public int getBirthday() {
		return birthday;
	}

	public void setBirthday(int birthday) {
		this.birthday = birthday;
	}

	public int getEdugrade() {
		return edugrade;
	}

	public void setEdugrade(int edugrade) {
		this.edugrade = edugrade;
	}

	public int getWorkyears() {
		return workyears;
	}

	public void setWorkyears(int workyears) {
		this.workyears = workyears;
	}

	public String getWorkExperience() {
		return workExperience;
	}

	public void setWorkExperience(String workExperience) {
		this.workExperience = workExperience;
	}

	public String getStudyExperience() {
		return studyExperience;
	}

	public void setStudyExperience(String studyExperience) {
		this.studyExperience = studyExperience;
	}

	public String getOldpass() {
		return oldpass;
	}

	public void setOldpass(String oldpass) {
		this.oldpass = oldpass;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	
	
	
	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception e) {
			logger.error(e);
			return ERROR;
		}
	}
	
	public String index(){
		setSexListValues();
		this.setUserInfo(this.getUserInfoService().findUserInfoByUid(userSession.getUid()));
		this.name = userInfo.getName();
		this.sex = userInfo.getUserDetail().getSex();
		this.mobile = userInfo.getUserDetail().getMobile();
		this.tel = userInfo.getUserDetail().getTel();
		this.qq = userInfo.getUserDetail().getQq();
		this.email = userInfo.getUserDetail().getEmail();
		return "index";
	}

	public String save(){
		UserInfo ui = this.getUserInfoService().findUserInfoByUid(userSession.getUid());
		ui.setName(this.name);
		UserDetail ud = ui.getUserDetail();
		ud.setMobile(this.mobile);
		ud.setTel(this.tel);
		ud.setQq(this.qq);
		ud.setEmail(this.email);
		ud.setSex(this.sex);
		ui.setUserDetail(ud);
		this.getUserInfoService().updateUserInfo(ui);
		return "save";
	}
	
	public String idcard(){
		this.setUserInfo(this.getUserInfoService().findUserInfoByUid(userSession.getUid()));
		return "idcard";
	}

	public String resume(){
		initResumeValues();
		Resume r = this.getUserInfoService().findResumeByUId(userSession.getUid());
		this.name = r.getName();
		this.mobile = r.getMobile();
		this.email = r.getEmail();
		this.sex = r.getSex();
		this.birthyear = r.getBirthyear();
		this.birthmonth = r.getBirthmonth();
		this.birthday = r.getBirthday();
		this.edugrade = r.getEdugrade();
		this.workyears = r.getWorkyears();
		this.workExperience = r.getWorkExperience();
		this.studyExperience = r.getStudyExperience();
		return "resume";
	}
	
	public String doresume(){
		Resume r = this.getUserInfoService().findResumeByUId(userSession.getUid());
		r.setName(this.name);
		r.setMobile(this.mobile);
		r.setEmail(this.email);
		r.setSex(this.sex);
		r.setBirthyear(this.birthyear);
		r.setBirthmonth(this.birthmonth);
		r.setBirthday(this.birthday);
		r.setEdugrade(this.edugrade);
		r.setWorkyears(this.workyears);
		r.setWorkExperience(this.workExperience);
		r.setStudyExperience(this.studyExperience);
		this.getUserInfoService().updateResume(r);
		return "doresume";
	}

	public String password(){
		this.setUserInfo(this.getUserInfoService().findUserInfoByUid(userSession.getUid()));
		return "password";
	}
	
	public String dopassword(){
		UserInfo ui = this.getUserInfoService().findUserInfoByUid(userSession.getUid());
		if(ui.getPassword().equals(this.oldpass)){
			ui.setPassword(this.password);
			ui.setRepassword(Util.hash(password));
			this.getUserInfoService().updateUserInfo(ui);
			this.getSession().put("code", "password_true");
		}else{
			this.getSession().put("code", "password_error");
		}
		return "password";
	}



}
